class FaultNode extends MyNode{
    constructor($state='new',...obj){
        super('Fault',$state,{
            name:"故障名",
            outId:1,
            suggest:"正常运行"
        },...obj);
    }

    fillWithRandomValue(){
        for (let i = 0; i < (this.matrix[0]+1)*(this.matrix[1]+1); i++) {
             if (!this.matrix[i+2]){
                 this.matrix[i+2] = Math.random();
             }
        }
    }

//  <div class="form-group">                
//      <label class="col-sm-2 control-label">故障模块</label>
//      <div class="col-sm-10">
//      <select class="form-control" 
//      data-ng-model="graph.getNode('${this.id}').faultModule" 
//      data-ng-options="x.name as x.name for x in graph.getNode('${this.id}').getModules()"
//      data-ng-init="graph.getNode('${this.id}').faultModule = '${this.faultModule}'"
//      ></select>
//      </div>
//  </div>

    getTemplate(edge,g){
        getCache("fault_module",val=>{
            FaultNode.modules=val.map(x=>{
                if(x.indexOf(':')<0){
                    return {
                        category:'',
                        name:x,
                    }
                }
                let arr = x.split(':',2);
                return ({
                    category:arr[0],
                    name:arr[1],
                })
            });
        },(callback,cache,name)=>{
            getConfigure('module',data=>{
                cache[name]={
                    lastTimestamp:new Date().getTime(),
                    value:data
                };
                callback(data);
            });
        },5000);

        return `
<div class="form-group">                
    <label class="col-sm-2 control-label">故障分组</label>
    <div class="col-sm-10">
    <select class="form-control" 
    data-ng-model="graph.getNode('${this.id}').faultCategory" 
    data-ng-options="x for x in ['汽轮发电机','水轮发电机','汽轮机','锅炉','水轮机','辅机','测试']"
    data-ng-init="graph.getNode('${this.id}').faultCategory = '${this.faultCategory}'"
    ></select>
    </div>
</div>
<div class="form-group">                
    <label class="col-sm-2 control-label">故障模块</label>
    <div class="col-sm-10">
	<input data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}" class="form-control" data-ng-model="graph.getNode('${this.id}').faultModule" placeholder="请输入模块ID">
    </div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label">故障ID</label>
    <div class="col-sm-10"><input data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}" type="text" class="form-control" data-ng-model="graph.getNode('${this.id}').outId" placeholder="用于导出的ID"></div>
</div>
<div class="form-group">
    <label class="col-sm-2 control-label">故障名称</label>
    <div class="col-sm-10">
	<input data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}" class="form-control" data-ng-model="graph.getNode('${this.id}').name" placeholder="请输入名称">
    </div>
 </div>
     <p class="col-sm-offset-2 col-sm-10 form-control-static help-block">
     <p>说明：添加已存在节点会自动进行存储操作，并刷新整个输入区内容</p>
        <a class="button btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachFeature('${this.id}')">添加特征</a>
        <a class="button btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachGroup('${this.id}','FeatureGroup')">添加特征组</a>
        <a class="button btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachExternalFeature('${this.id}')">添加已存在特征</a>
        <a class="button btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachExternalGroup('${this.id}','FeatureGroup')">添加已存在特征组</a>
        <a class="button btn" data-ng-if="allowCause('${this.id}')" data-ng-click="attachReason('${this.id}')">添加原因</a>
        <a class="button btn" data-ng-if="allowCause('${this.id}')" data-ng-click="attachGroup('${this.id}','ReasonGroup')">添加原因组</a>
    </p>
 </div>
`;
    // <div class="form-group">
    //         <label class="col-sm-2 control-label">概率表&nbsp;
    // <a class="button btn" data-ng-click="graph.getNode('${this.id}').fillWithRandomValue()">填充随机概率</a>
    //         </label>
    //         <div class="col-sm-10">
    //         <table class="table table-responsive table-bordered" ng-init="fs=graph.getFeatures();rs=graph.getReasons();nf=fs.length;nr=rs.length;graph.getNode('${this.id}').matrix[0] = nf;graph.getNode('${this.id}').matrix[1] = nr;">
    //         <thead>
    //         <tr><td>特征/原因</td><td ng-repeat="reason in rs track by $index">{{reason.getText()}}</td><td>整体</td></tr>
    //     </thead>
    //     <tbody>
    //     <tr ng-repeat="feature in fs track by $index" ng-init="$index2=$index">
    //         <td>{{feature.getText()}}</td><td ng-repeat="reason in rs track by $index">
    //     <input class="form-control" data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}" data-ng-model="graph.getNode('${this.id}').matrix[$index2*(nr+1)+$index+2]">
    //         </td><td> <input class="form-control" data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}" data-ng-model="graph.getNode('${this.id}').matrix[($index2+1)*(nr+1)+1]"></td>
    //     </tr>
    //     </tbody>
    //     <tfoot><tr>
    //     <td>整体</td><td ng-repeat="reason in rs track by $index">
    //     <input class="form-control" data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}" data-ng-model="graph.getNode('${this.id}').matrix[nf*(nr+1)+$index+2]">
    //         </td><td><input class="form-control" data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}" data-ng-model="graph.getNode('${this.id}').matrix[(nf+1)*(nr+1)+1]"></td>
    //     </tr></tfoot>
    //     </table>
    //     </div>
    //     </div>
    }

    static loadFaults(){
        let session = neo4jService.getSession();
        let resultPromise = session.run(`MATCH (n:Fault) RETURN n`);
        resultPromise.then(result => {
            session.close();
            FaultNode.faults = result.records
                .map((record) => record.get(0))
                .map(node => MyNode.fromNeo4jNode(node));
            for (let fn of FaultNode.loadFaults.callbacks){
                fn(FaultNode.faults);
            }
        });
    }

    getText(){
        let sIndex = '';
        if(this.outId!=0)
            sIndex = `<${this.outId}>`;
        return `故障${sIndex}：${this.name}`;
    }

    getTypeName(){
        return "故障";
    }

    getModules(){
        return FaultNode.modules;
    }
}
FaultNode.faults=[];
FaultNode.modules=[];
// FaultNode.loadFaults();
FaultNode.loadFaults.callbacks=[];
